import http from "@/utils/http";
/**
*add by wuyx 销售品明细保存
*/
const pageOfferDetailSave = {
    actTypeMod : "MOD",
    actTypeAdd : "ADD",
    /**
     * 构建明细保存对象
     * @param {*} res 
     * @param {*} optionId 
     * @param {*} orderItemId 
     * @param {*} custId 
     * @returns 
     */
    buildObject: (res,optionId,orderItemId,custId) => {
        // let prodSaveInfo = {}; //存放接口报文
        // let contractRoot = {};
        // let svcCont = {};
        
        let requestObject = {};
        requestObject.optionId = ""+optionId;
        requestObject.orderItemId =""+orderItemId;
        requestObject.custId = ""+custId;
        requestObject.customerOrders =pageOfferDetailSave.buildAttrs(res,'CUSTOMER_ORDER','custOrderId',custId);
        requestObject.orderItems=pageOfferDetailSave.buildAttrs(res,'ORDER_ITEM','orderItemId',custId);
        requestObject.ordDevStaffInfos=pageOfferDetailSave.buildAttrs(res,'ORD_DEV_STAFF_INFO','rowId',custId);
        requestObject.orderItemAttrs=pageOfferDetailSave.buildAttrs(res,'ORDER_ITEM_ATTR','orderItemAttrId',custId);
        requestObject.orderContactInfos=pageOfferDetailSave.buildAttrs(res,'ORDER_CONTACT_INFO','orderContactInfoId',custId);
        requestObject.ordProdInsts=pageOfferDetailSave.buildAttrs(res,'ORD_PROD_INST','rowId',custId);
        requestObject.ordProdInstAttrs=pageOfferDetailSave.buildAttrs(res,'ORD_PROD_INST_ATTR','rowId',custId);
        requestObject.orderTasks=pageOfferDetailSave.buildAttrs(res,'ORDER_TASK','orderTaskId',custId);
        requestObject.orderTaskAttrs=pageOfferDetailSave.buildAttrs(res,'ORDER_TASK_ATTR','orderTaskAttrId',custId);
        requestObject.acctItems=pageOfferDetailSave.buildAttrs(res,'ACCT_ITEM','acctItemId',custId);
        requestObject.acctItemOrgs=pageOfferDetailSave.buildAttrs(res,'ACCT_ITEM_ORG','acctItemOrgId',custId);
        requestObject.ordOfferInstFeeInfos=pageOfferDetailSave.buildAttrs(res,'ORD_OFFER_INST_FEE_INFO','rowId',custId);
        requestObject.ordProdInstAcctRels=pageOfferDetailSave.buildAttrs(res,'ORD_PROD_INST_ACCT_REL','rowId',custId);
        requestObject.ordOfferInstFeeAttrs=pageOfferDetailSave.buildAttrs(res,'ORD_OFFER_INST_FEE_ATTR','rowId',custId);
        return requestObject;
    },
    /**
     * 构造修改项属性信息
     * @param {*} res 
     * @returns 
     */
    buildAttrs:(res,tableName,instIdFiled,custId) =>{
        let insts=[];
        let objMap=new Map();
        for (let key in res) {
            let item = res[key];
            if(item.tableName==tableName){
               if(item.value!=item.oldValue&&((item.instId!=undefined&&item.instId!="")||(item.rowId!=undefined&&item.rowId!=""))){
                     if(item.rowIds||item.instIds){
                        let instIds=item.rowIds!=undefined?item.rowIds.split(";"):item.instIds.split(";");
                        if(instIds.length>1){
                            instIds.forEach((id)=>{
                                pageOfferDetailSave.buildObj(objMap,item,id,instIdFiled,custId);
                            })
                        }else{
                            let instId= item.rowId!=undefined?item.rowId:item.instId;
                            pageOfferDetailSave.buildObj(objMap,item,instId,instIdFiled,custId); 
                        }

                     }else{
                        let instId= item.rowId!=undefined?item.rowId:item.instId;
                        pageOfferDetailSave.buildObj(objMap,item,instId,instIdFiled,custId);
                     }               

               }
            }

        }
        objMap.forEach((val,key)=>{
            insts.push(val);
        });
        return insts;

    },
    /**
     * 构建修改项对象
     * @param {*} objMap 对象缓存容器
     * @param {*} item   formdata 项
     * @param {*} instId 实例主键
     * @param {*} instIdFiled 主键字段名称
     * @param {*} custId 客户名称
     */
    buildObj:(objMap,item,instId,instIdFiled,custId)=>{
        let obj=objMap.get(instId);
        if(obj!=null&&obj!=undefined){
            obj[item.colId]=item.value;
        }else{
            obj={};
            let colType=item.colType;
            if(colType=="T"){
                obj['attrValue']=item.value;
            }else{
                obj[item.colId]=item.value;
            }   
            obj['actType']=pageOfferDetailSave.actTypeMod;
            obj['custId']=custId;
            obj[instIdFiled]=instId;
            objMap.set(instId,obj);
      }
    },
    /**
     * 获取变更项属性信息 
     * @param {} attrArr 
     * @returns 
     */
    getOrderItemAttrModifys: (attrArr) => {
		let orderItemAttrModifyArr = [];
		attrArr.forEach((item) => {
			let obj = {};
			if (item.tableName && item.colId && item.value != '' && item.value != null) {
				if ((item.oldValue && item.oldValue != item.value) || !item.oldValue) {
					let attrid = '';
					if (item.id.split('-').length > 1) {
						attrid = item.id.split('-')[1];
					} else {
						attrid = item.id;
					}
					obj.attrId = attrid;
					obj.attrValue = item.value;
					obj.attrCd = item.fieldId;
					if (item.attrRoleCd) {
						obj.roleCd = item.attrRoleCd;
					} else {
						obj.roleCd = '0';
					}
					orderItemAttrModifyArr.push(obj)
				}
			}
		});
		return orderItemAttrModifyArr;
	},
    /**
     * 获取多个实例ID
     * @param {*} res 
     * @param {*} instItem 
     * @returns 
     */
    getMultipleInstIds:(res,instItem)=>{
        let ids=[]
        if(!instItem.attrRoleCd){
            for (let key in res) {
                let item = res[key];
                if(item.relIds){
                    let relIds=item.relIds;
                    let mapArr = relIds.split(";")
                    if(mapArr.includes(instItem.id)){
                        
                        ids.push(item.instId);
                    }
                }
            }
        }
        return ids;
    },
    /**
     * 订单明细修改后台接口调用
     * @param {*} param 
     * @returns 
     */
    async modOrderInfoByOption(param) {
        let res = await http({
            url: `${process.env.VUE_APP_CTX_ORDER_QUERY}/modOrderInfoByOption`,
            data: param,
            method: "POST",
        });
        return res;
    }
   


};

export default pageOfferDetailSave;
